package com.view;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.*;
import java.io.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.*;
import javax.swing.*;

public class PanelTambahFotoAnggota extends JPanel {
    private JLabel labelKodeAnggota;
    private JTextField textKodeAnggota;
    private JButton tombolSimpan, tombolCari;
    private File file;
    private BufferedImage image = null;
    private JPanel panelTombol; 
    
    public PanelTambahFotoAnggota() {
        inisialisasi();
    }
    
    public void inisialisasi() {
        file = new File("1412.png");
        try {
            image = ImageIO.read(file);
            image.getGraphics(); 
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        setLayout(new BorderLayout());
        JPanel panelTombol = new JPanel();
        panelTombol.setLayout(new FlowLayout());
            labelKodeAnggota = new JLabel("Name: ");
                panelTombol.add(labelKodeAnggota);
            textKodeAnggota = new JTextField();
                textKodeAnggota.setColumns(20);
                panelTombol.add(textKodeAnggota);
                textKodeAnggota.addActionListener(new ActionListener() {
                @Override   public void actionPerformed(ActionEvent e) {
                                textKodeAnggotaAction(); 
                            }}); 
            tombolCari = new JButton("Browse");
                panelTombol.add(tombolCari);
                tombolCari.addActionListener(new ActionListener() {
                @Override   public void actionPerformed(ActionEvent e) {
                                tombolCariGambarAction(); 
                            }}); 
            tombolSimpan = new JButton("Save");
                panelTombol.add(tombolSimpan);
                tombolSimpan.addActionListener(new ActionListener() {
                @Override   public void actionPerformed(ActionEvent e) {
                                tombolSimpanAction(); 
                            }}); 
            add(panelTombol, BorderLayout.SOUTH);
    }
    
    public void tombolSimpanAction() {
        File fileBaru = new File(textKodeAnggota.getText());
        try {
            FileOutputStream os = new FileOutputStream(fileBaru);
            FileInputStream is = new FileInputStream(file);
        } catch (FileNotFoundException ex) {
            Logger.getLogger(PanelTambahFotoAnggota.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    public void tombolCariGambarAction() {
        JFileChooser chooser = new JFileChooser();
        int status = chooser.showOpenDialog(this);
//        if(status == APPROVE_ACTION){
//            file = chooser.getSelectedFile();
//            repaint(); 
//        }
    }
    
    public void textKodeAnggotaAction() {
        File file = new File(textKodeAnggota.getText() + ".PNG");
        repaint();
    }
    
    @Override
    public void paintComponent(Graphics g) {
        g.drawImage(image,20, 20, 200, 270,0, 0, image.getWidth(null), image.getHeight(null),null);
    }
}